#pragma once
#include <iostream>
#include <string>
#include<unistd.h>
#include <pthread.h>

using namespace std;

enum Status
{
    NEW,
    RUNNING,
    DESTORY
};

typedef void (*func_t)(void *);
class thread
{
public:
    //构造
    thread(int num,func_t func,void* args)
        :_status(NEW),_tid(0),_func(func),_args(args)
    {
        char *buf = new char[64];
        snprintf(buf,64,"thread_%d",num);
        _name = static_cast<string>(buf);
    }

    void operator()()
    {
        if(_func) _func(_args);
    }

    //create
    static void* runHelper(void* args)
    {
        thread *ts = (thread*)args;
        (*ts)();
        return nullptr;
    }

    void run()
    {
        int n = pthread_create(&_tid,nullptr,runHelper,(void*)this);
       
        sleep(1);
        if(n)
            perror("pthread_create: ");
        _status = RUNNING;
    }
    //join
    void join()
    {
        pthread_join(_tid,nullptr);
        _status = DESTORY;
        cout<<"wait success!"<<endl;
    }

private:
    Status _status;//状态
    pthread_t _tid;//获取tid
    string _name;//输出线程名
    func_t _func;//线程使用方法
    void* _args;
};